package com.gtworldmobile.activities;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import org.mixare.R;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;

import com.gtworld.android.slideout.SlideoutActivity;
import com.gtworldmobile.design.PagerAdapter;
import com.gtworldmobile.tabs.MiCuentaDatos;
import com.gtworldmobile.tabs.MiCuentaEditar;
import com.gtworldmobile.tabs.MiCuentaPrefer;

public class MiCuentaActivity extends FragmentActivity implements
		TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

	private TabHost mTabHost;
	private ViewPager mViewPager;
	private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, MiCuentaActivity.TabInfo>();
	private PagerAdapter mPagerAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mi_cuenta);
		findViewById(R.id.menu_button).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View v) {
						int width = (int) TypedValue.applyDimension(
								TypedValue.COMPLEX_UNIT_DIP, 40, getResources()
										.getDisplayMetrics());
						SlideoutActivity.prepare(MiCuentaActivity.this,
								R.id.inner_content, width);
						startActivity(new Intent(MiCuentaActivity.this,
								MenuActivity.class));
						overridePendingTransition(0, 0);
					}
				});
		this.initialiseTabHost(savedInstanceState);
		this.intialiseViewPager();
		//this.setTabsLayout(30);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			int width = (int) TypedValue.applyDimension(
					TypedValue.COMPLEX_UNIT_DIP, 40, getResources()
							.getDisplayMetrics());
			SlideoutActivity.prepare(MiCuentaActivity.this, R.id.inner_content,
					width);
			startActivity(new Intent(MiCuentaActivity.this, MenuActivity.class));
			overridePendingTransition(0, 0);
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(true);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private void intialiseViewPager() {

		List<Fragment> fragments = new Vector<Fragment>();
		fragments
				.add(Fragment.instantiate(this, MiCuentaDatos.class.getName()));
		fragments
				.add(Fragment.instantiate(this, MiCuentaEditar.class.getName()));
		fragments
				.add(Fragment.instantiate(this, MiCuentaPrefer.class.getName()));
		this.mPagerAdapter = new PagerAdapter(
				super.getSupportFragmentManager(), fragments);
		//
		this.mViewPager = (ViewPager) super.findViewById(R.id.miCuentaPager);
		this.mViewPager.setAdapter(this.mPagerAdapter);
		this.mViewPager.setOnPageChangeListener(this);
	}

	private void initialiseTabHost(Bundle args) {
		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup();
		TabInfo tabInfo = null;
		MiCuentaActivity.AddTab(this, this.mTabHost,
				this.mTabHost.newTabSpec("Tab1").setIndicator("Datos"),
				(tabInfo = new TabInfo("Tab1", MiCuentaDatos.class, args)));
		this.mapTabInfo.put(tabInfo.tag, tabInfo);
		MiCuentaActivity.AddTab(this, this.mTabHost,
				this.mTabHost.newTabSpec("Tab2").setIndicator("Editar"),
				(tabInfo = new TabInfo("Tab2", MiCuentaEditar.class, args)));
		this.mapTabInfo.put(tabInfo.tag, tabInfo);
		MiCuentaActivity.AddTab(this, this.mTabHost,
				this.mTabHost.newTabSpec("Tab3").setIndicator("Preferencias"),
				(tabInfo = new TabInfo("Tab3", MiCuentaPrefer.class, args)));
		this.mapTabInfo.put(tabInfo.tag, tabInfo);
		// Default to first tab
		// this.onTabChanged("Tab1");
		//
		mTabHost.setOnTabChangedListener(this);
	}

	private static void AddTab(MiCuentaActivity activity, TabHost tabHost,
			TabHost.TabSpec tabSpec, TabInfo tabInfo) {
		// Attach a Tab view factory to the spec
		tabSpec.setContent(activity.new TabFactory(activity));
		tabHost.addTab(tabSpec);
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		this.mTabHost.setCurrentTab(position);

	}

	@Override
	public void onTabChanged(String tabId) {
		int pos = this.mTabHost.getCurrentTab();
		this.mViewPager.setCurrentItem(pos);

	}

	/*private void setTabsLayout(int height) {

		mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
		mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
		mTabHost.getTabWidget().getChildAt(2).getLayoutParams().height = height;

	}*/

	private class TabInfo {
		private String tag;
		@SuppressWarnings("unused")
		private Class<?> clss;
		@SuppressWarnings("unused")
		private Bundle args;
		@SuppressWarnings("unused")
		private Fragment fragment;

		TabInfo(String tag, Class<?> clazz, Bundle args) {
			this.tag = tag;
			this.clss = clazz;
			this.args = args;
		}

	}

	class TabFactory implements TabContentFactory {

		private final Context mContext;

		/**
		 * @param context
		 */
		public TabFactory(Context context) {
			mContext = context;
		}

		/**
		 * (non-Javadoc)
		 * 
		 * @see android.widget.TabHost.TabContentFactory#createTabContent(java.lang.String)
		 */
		public View createTabContent(String tag) {
			View v = new View(mContext);
			v.setMinimumWidth(0);
			v.setMinimumHeight(0);
			return v;
		}

	}

}
